<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    width="50%"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form
      :model="form"
      :rules="rules"
      :disabled="mode == 'show'"
      ref="dialogForm"
      label-width="auto"
      label-position="right"
    >
      <el-form-item label="送货名称" prop="name">
        <el-input
          v-model="form.name"
          rows="3"
          clearable
          placeholder="请输入送货名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="客户名称" prop="customerId">
        <el-select
          v-model="form.customerId"
          placeholder="请选择客户"
          style="width: 100%"
          remote
          filterable
          remote-show-suffix
          :remote-method="queryCustomer"
          :loading="selectCustomerLoading"
          clearable
        >
          <el-option
            v-for="item in customerList"
            :key="item.id"
            :label="item.customerName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="生产单" prop="produceId">
        <el-select
          v-model="form.produceId"
          placeholder="请选择生产单"
          style="width: 100%"
          remote
          filterable
          remote-show-suffix
          :remote-method="queryProduce"
          :loading="selectProduceLoading"
          clearable
        >
          <el-option
            v-for="item in produceList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="配送人" prop="deliveryId">
        <el-select
          v-model="form.deliveryId"
          placeholder="请选择配送人"
          style="width: 100%"
          remote
          filterable
          remote-show-suffix
          :remote-method="queryUser"
          :loading="selectLoading"
          clearable
        >
          <el-option
            v-for="item in userList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="送货状态" prop="type">
        <el-select
          v-model="form.type"
          placeholder="请选择送货状态"
          style="width: 100%"
          clearable
        >
          <el-option
            v-for="item in deliverStataus"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="配送时间" prop="deliverTime">
        <el-date-picker
          v-model="form.deliverTime"
          type="datetime"
          placeholder="请选择配送时间"
          value-format="YYYY-MM-DD HH:mm:ss"
          :disabled-date="disabledDate"
          style="width: 100%"
        />
      </el-form-item>
      <el-form-item label="收货地址" prop="address" style="width: 100%">
        <el-input
          v-model="form.address"
          rows="3"
          clearable
          type="textarea"
          placeholder="请输入收货地址"
        ></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark" style="width: 100%">
        <el-input
          v-model="form.remark"
          rows="3"
          clearable
          type="textarea"
          placeholder="请输入备注内容"
        ></el-input>
      </el-form-item>
    </el-form>

    <!-- 审核流程项 -->
    <check-form-item
      ref="checkFormItem"
      @setCheckConfig="setCheckConfig"
      :field="this.form"
      :checkType="checkType"
      v-if="mode == 'add'"
    ></check-form-item>

    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button
        v-if="mode != 'show'"
        type="primary"
        :loading="isSaveing"
        @click="submit()"
        >保存</el-button
      >
    </template>
  </el-dialog>
</template>

<script>
import { queryTypeByDic } from '@/utils/getData'
import checkFormItem from '@/components/checkProcess/checkFormItem.vue'
export default {
  emits: ['success', 'closed'],
  data () {
    return {
      checkType: 4,//工作流的类型
      mode: "add",
      titleMap: {
        add: '新增',
        edit: '编辑',
        show: '查看'
      },
      visible: false,
      isSaveing: false,
      selectCustomerLoading: false,
      selectProduceLoading: false,
      selectLoading: false,
      //表单数据
      form: {
        adminId: '',//跟进人id --登录时id
        departmentId: '',
        produceId: '',//生产单id
        customerId: "",//客户id
        name: '',//送货名称
        address: '',//收货地址
        deliveryId: '',//配送人id
        deliverTime: '',//配送时间
        remark: '',//备注
        workflowJsonList: []
      },
      deliverStataus: [],//送货状态
      customerList: [],//客户列表
      produceList: [],//生产单列表
      userList: [],
      //验证规则
      rules: {
        name: [
          { required: true, message: '请选择送货名称', }
        ],
        customerId: [
          { required: true, message: '请选择客户', trigger: 'change' }
        ],
        produceId: [
          { required: true, message: '请选择生产单', trigger: 'change' }
        ],
        deliveryId: [
          { required: true, message: '请选择配送人', trigger: 'change' }
        ],
        address: [
          { required: true, message: '请输入收货地址' }
        ],
        type: [
          { required: true, message: '请选择送货状态', trigger: 'change' }
        ],
        deliverTime: [
          { type: 'date', required: true, message: '请选择配送时间', trigger: 'change', },
        ],
        userId: [
          { required: true, message: '请选择负责人', trigger: 'change' }
        ],

      }
    }
  },
  components: {
    checkFormItem
  },
  mounted () {
    this.getCustomerList()
    this.getProduceList()
    this.getUserList()
    this.getType()
  },
  methods: {
    // 获取送货状态
    async getType () {
      this.deliverStataus = await queryTypeByDic('deliverStatus')
    },
    // 获取客户列表
    async getCustomerList (val) {
      this.selectCustomerLoading = true
      let param = {
        limit: 1000,
        page: 1,
        search: val
      }
      let res = await this.$API.customer.getCustomers.get(param)
      this.selectCustomerLoading = false
      this.customerList = res.data
    },
    // 搜索客户
    queryCustomer (val) {
      this.getCustomerList(val)
    },
    // 获取生产单列表
    async getProduceList (val) {
      this.selectProduceLoading = true
      let data = {
        limit: 1000,
        page: 1,
        search: val
      }
      let res = await this.$API.produce.getProduceOrderList.get(data)
      this.selectProduceLoading = false
      this.produceList = res.data
    },
    // 搜索生产单
    queryProduce (val) {
      this.getProduceList(val)
    },
    // 获取所有用户列表
    async getUserList (val) {
      let param = {
        limit: 1000,
        page: 1,
        search: val
      }
      let res = await this.$API.user.getUserList.get(param)
      this.userList = res.data
    },
    // 搜索用户
    queryUser (val) {
      this.getUserList(val)
    },

    // 审批流配置
    setCheckConfig (list) {
      this.form.workflowJsonList = list
    },
    //显示
    open (mode = 'add') {
      this.mode = mode
      this.visible = true
      let userInfo = this.$TOOL.data.get('USER_INFO')
      this.form.departmentId = userInfo.departmentId
      this.form.adminId = userInfo.userId
      return this
    },
    //表单提交方法
    submit () {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true
          let res = null
          if (this.mode == 'add') {
            res = await this.$API.produce.deliver.addDeliver.post(this.form)
          } else if (this.mode == 'edit') {
            delete this.form.status
            delete this.form.cause
            delete this.form.createTime
            delete this.form.updateTime
            delete this.form.auditTime
            res = await this.$API.produce.deliver.updateDeliver.post(this.form)
          }
          this.isSaveing = false
          if (res.code == 200) {
            this.$emit('success', this.form, this.mode)
            this.visible = false
            this.$message.success("操作成功")
          }
        }
      })
    },
    //表单注入数据
    setData (data) {
      this.form = data

    },
    // 设置就日期选择范围  今天之后
    disabledDate (time) {
      return time.getTime() < Date.now() - 8.64e7//如果没有后面的-8.64e7就是不可以选择
    }
  }
}
</script>

